//
//  NSString+JSONCategories.m
//  OCUI
//
//  Created by login on 15-8-18.
//
//

#import "NSString+JSONCategories.h"
#import "NSBaseBean.h"
@implementation NSString(JSONCategories)
-(id)JSONValue;
{
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    __autoreleasing NSError* error = nil;
    //id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    if (error != nil) return nil;
    return [self toArrayOrNSDictionary:data];
}
-(id)JSONModelMap:(NSString*)keyName WithKeyValue: (NSString*)KeyValue
{
    NSArray * arr=(NSArray*)[self JSONValue];
    NSBaseBean * bean=[[NSBaseBean alloc] init];
    bean.code=[(NSDictionary*)arr[0] objectForKey:keyName];
    bean.value=[(NSDictionary*)arr[1] objectForKey:KeyValue];
    return bean;
}
- (id) toArrayOrNSDictionary:(NSData *)jsonData{
    @try
  {
    NSError *error = nil;
    id jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData
                                                          options:NSJSONReadingAllowFragments
                                                            error:nil];
    if (jsonArray != nil && error == nil){
        return jsonArray;
    }else{
        // 解析错误
        return nil;
    }
    
  }
 @catch(NSException * e)
{
    NSLog(@"%@",e.description);
    return nil;
}
}
@end
